package com.example.demo02.controller.Medicine;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.demo02.entity.Car;
import com.example.demo02.entity.Medicine;
import com.example.demo02.entity.Shopping;
import com.example.demo02.service.CarService;
import com.example.demo02.service.MedicineService;
import com.example.demo02.service.ShoppingService;
import com.example.demo02.utils.FileUtil;
import com.example.demo02.utils.ResponseData;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Tag(name = "药品信息接口")
@RestController
@RequestMapping("/Medicine")
public class MedicineController {
    @Resource
    private MedicineService medicineService;
    @Resource
    private ShoppingService shoppingService;
    @Resource
    private CarService carService;

    //为数据可视化增加的接口
    @PostMapping("/postMedicine")
    public ResponseData postMedicine(@Param("id") String id) {
        List<Medicine> medicineList = medicineService.list();
        return new ResponseData("200", null, true, medicineList);
    }

    @GetMapping("/findAllPage")
    public ResponseData findAllPage(@RequestParam(value = "start",defaultValue = "1")int start, @RequestParam(value = "size",defaultValue = "3")int size) {
        //表示起始页维start，每页显示size条记录 根据id升序进行分页
        IPage<Medicine> page = medicineService.getPage(start,size);
        return new ResponseData("200", null, true, page);
    }

    @GetMapping("/findAllMedicine")
    public ResponseData findAllMedicine() {
        List<Medicine> medicineList = medicineService.list();
        return new ResponseData("200", null, true, medicineList);
    }

    @GetMapping("/findMedicineById/{id}")
    public ResponseData findMedicineById(@PathVariable int id) {
        Medicine medicine = medicineService.getById(id);
        return new ResponseData("200", null, true, medicine);
    }

    @PostMapping("/addMedicine")
    public ResponseData addMedicine(@RequestBody Medicine medicine) {
        medicineService.save(medicine);
        return new ResponseData("200", null, true, "药品添加成功");
    }

    @DeleteMapping("/deleteMedicine/{id}")
    public ResponseData deleteMedicine(@PathVariable int id) {
        medicineService.removeById(id);
        return new ResponseData("200", null, true, "药品删除成功");
    }

    @PutMapping("/updateMedicine")
    public ResponseData updateMedicine(@RequestBody Medicine medicine) {
        medicineService.updateById(medicine);
        return new ResponseData("200", null, true, "药品修改成功");
    }

    @PostMapping("/addToShopping")
    public ResponseData addToShopping(@RequestBody Shopping shopping) {
        Medicine medicine = medicineService.getById(shopping.getMedicineId());
        medicine.setNum(medicine.getNum() - shopping.getMedicineNum());
        medicineService.updateById(medicine);
        Shopping shopping1 = new Shopping();
        shopping1.setMedicineId(shopping.getMedicineId());
        shopping1.setMedicineName(shopping.getMedicineName());
        shopping1.setMedicinePrice(shopping.getMedicinePrice());
        shopping1.setMedicineNum(shopping.getMedicineNum());
        shopping1.setTotalPrice(shopping.getMedicinePrice() * shopping.getMedicineNum());
        shoppingService.save(shopping1);
        return new ResponseData("200", null, true, "购物车添加成功");

    }

    @DeleteMapping("/deleteShopping/{id}")
    public ResponseData deleteShopping(@PathVariable int id) {
        boolean delete = shoppingService.removeById(id);
        if (delete) {
            return new ResponseData("200", null, true, "删除成功");
        } else {
            return new ResponseData("500", "删除失败", true, "删除失败");
        }
    }

    @GetMapping("/findAllShoppingList")
    public ResponseData findAllShoppingList() {
        List<Shopping> shoppingList = shoppingService.list();
        System.out.println(shoppingList);
        return new ResponseData("200", null, true, shoppingList);
    }

    @PostMapping("/addToCar")
    public ResponseData addCar(@RequestBody Car car) {
        carService.save(car);
        return new ResponseData("200", null, true, "购物车添加成功");
    }

    @GetMapping("/findAllCarList")
    public ResponseData findAllCarList() {
        List<Car> carList = carService.list();
        System.out.println(carList);
        return new ResponseData("200", null, true, carList);
    }

    @DeleteMapping("/deleteCar/{id}")
    public ResponseData deleteCar(@PathVariable int id) {
        boolean delete = carService.removeById(id);
        if (delete) {
            return new ResponseData("200", null, true, "删除成功");
        } else {
            return new ResponseData("500", "删除失败", true, "删除失败");
        }
    }

    //商品图片上传
    @PostMapping("/fileUpload")
    public String fileUpload(@RequestParam("uploadFile") MultipartFile multipartFile) throws IOException {
        return FileUtil.uploadFile(multipartFile);
    }
}
